using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Text;

namespace TmxLib
{
    public class TmxHeader
    {
        public string DefaultCultureName;
        public TmxLanguageCollection Cultures = new TmxLanguageCollection();
        public string ContextSeparator;

        public TmxHeader()
        {
        }

        public TmxHeader(string defaultCultureName)
        {
            DefaultCultureName = defaultCultureName;
        }
    }

    public class TmxLanguage
    {
        public string CultureName;
        public bool IsReadOnly;
        public bool IsRequired;

        public TmxLanguage(string cultureName, bool isReadOnly)
        {
            CultureName = cultureName;
            IsReadOnly = isReadOnly;
        }

        public string EnglishName
        {
            get
            {
                return new CultureInfo(CultureName).EnglishName + " (" + CultureName + ")";
            }
        }

        public TmxLanguage Clone()
        {
            return (TmxLanguage) this.MemberwiseClone();
        }
    }

    public class TmxLanguageCollection: Collection<TmxLanguage>
    {
        public TmxLanguageCollection()
        {
        }

        public TmxLanguageCollection(IEnumerable<TmxLanguage> list) 
        {
            foreach(TmxLanguage lang in list)
                Add(lang);
        }

        public TmxLanguageCollection Clone()
        {
            TmxLanguageCollection ret = new TmxLanguageCollection();
            foreach(TmxLanguage lang in this)
                ret.Add(lang.Clone());

            return ret;
        }

        public string AsXmlString
        {
            get
            {
                StringBuilder ret = new StringBuilder();
                foreach(TmxLanguage lang in this)
                {
                    if (ret.Length > 0)
                        ret.Append(";");

                    ret.Append(lang.CultureName);
                    if (lang.IsRequired)
                        ret.Append("!");

                    ret.Append(lang.IsReadOnly ? "-" : "+");
                }

                return ret.ToString();
            }
        }

        public static TmxLanguageCollection Parse(string raw)
        {
            TmxLanguageCollection ret = new TmxLanguageCollection();
            string[] arr = raw.Split(';');
            foreach(string lraw in arr)
            {
                string lraw2 = lraw.Trim();
                TmxLanguage lang = null;
                if (lraw2.EndsWith("+") && lraw2.Length > 1)
                {
                    lang = new TmxLanguage(lraw2.Substring(0,lraw2.Length - 1), false);
                }

                else if (lraw2.EndsWith("-") && lraw2.Length > 1)
                {
                    lang = new TmxLanguage(lraw2.Substring(0, lraw2.Length - 1), true);
                }

                else if (lraw.Length == 0)
                {
                    lang = null;
                }

                else
                {
                    lang = new TmxLanguage(lraw2, true);
                }

                if (lang != null)
                    ret.Add(lang);
            }

            return ret;
        }

        public string ToLongString()
        {
            StringBuilder ret = new StringBuilder();
            foreach (TmxLanguage lang in this)
            {
                if (ret.Length > 0)
                    ret.Append(";");

                ret.Append(lang.EnglishName);
            }

            return ret.ToString();
        }
    }


}
